package bancoperu.rest;


import java.util.Date;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONObject;
import bancoperu.dao.AccountDAO;
import bancoperu.dao.TransactionDAO;
import bancoperu.model.Account;
import bancoperu.model.Customer;
import bancoperu.model.Transaction;

@Path("/account")
public class AccountRest {


	
	@POST
	@Path("/transfer")
	@Produces(MediaType.TEXT_PLAIN)
	public String loginCustomer( @FormParam("customerID") String customerID ,
								 @FormParam("accountType") int accountType,
								 @FormParam("amount") double amount
									) {		
	/*
	@GET
	@Path("/transfer")
	@Produces(MediaType.TEXT_PLAIN)
	public String loginCustomer( @QueryParam("customerID") String customerID ,
								@QueryParam("accountType") int accountType,
								@QueryParam("amount") double amount
									) {
		*/
		System.out.println(customerID);
		System.out.println(accountType);
		System.out.println(amount);
		
		JSONObject objeto = new JSONObject();
		
		try {
			
			
			
			
			
			
			
			// Leer el account balance actual
			AccountDAO accdao = new AccountDAO();
			
			Account current = accdao.GetAccountBalance2(customerID, accountType);
			double plata = current.getAccountBalance();
			
			if(accountType == 1)
			{
				current = accdao.GetAccountBalance2(customerID, 2);
			}
			else
			{
				current = accdao.GetAccountBalance2(customerID, 1);
			}
			
			double plata2 = current.getAccountBalance();
			
			
			double gana  = plata2 + amount;
			double pierde = plata - amount;
			
			
			AccountDAO dao = new AccountDAO();
			
			
			Customer cvo = new Customer();
			cvo.setCustomerId(customerID);
			
			
			Account vo = new Account();
			vo.setAccountType(accountType);
			vo.setCustomer(cvo);
			
			// checking
			if (accountType == 1 ){
				
				
				vo.setAccountType(2);
				vo.setAccountBalance(gana);
				
				dao.update(vo);
				
				
				vo.setAccountType(1);
				vo.setAccountBalance(pierde);
				
				dao.update(vo);
				
// INSERTANDO EN TRANSACTION		
				
				Account accountxx = new Account();
				accountxx.setAccountID(1+"");
				
				Account account2xxx = new Account();
				account2xxx.setAccountID(2+"");
				
				Transaction tvo = new Transaction();
				tvo.setAccount(accountxx);
				tvo.setCustomer(cvo);
				tvo.setType(3);
				tvo.setTransaction(new Date());
				tvo.setAmount(amount);
				tvo.setAccount2(account2xxx);				
				
				TransactionDAO tdao = new TransactionDAO();
				tdao.insert(tvo);
				
				// FIN INSERTANDO EN TRANSACTION
				
				
				
			// saving
			} else if (accountType == 2) {

				vo.setAccountType(1);
				vo.setAccountBalance(gana);
				
				dao.update(vo);
				
				vo.setAccountType(2);
				vo.setAccountBalance(pierde);
				
				dao.update(vo);
// INSERTANDO EN TRANSACTION		
				
				Account accountxx = new Account();
				accountxx.setAccountID(1+"");
				
				Account account2xxx = new Account();
				account2xxx.setAccountID(2+"");
				
				Transaction tvo = new Transaction();
				tvo.setAccount(accountxx);
				tvo.setCustomer(cvo);
				tvo.setType(3);
				tvo.setTransaction(new Date());
				tvo.setAmount(amount);
				tvo.setAccount2(account2xxx);				
				
				TransactionDAO tdao = new TransactionDAO();
				tdao.insert(tvo);
				
				// FIN INSERTANDO EN TRANSACTION
				
				
				
			}
			
				
			// AccountBalance=? WHERE CustomerID = ? and AccountType=?
			
			
			
			
			
			
			objeto.put("status", "true" );
			
			
			
		} catch (Exception e) {	
			e.printStackTrace();
			System.out.println("////" + e.getMessage());
			
			objeto.put("status", "false");
		}
		return objeto.toString();
	}
	
	
	
	
}
